package kr.co.graviti.common.util;

import java.util.HashMap;

public class PagingUtil {

	private int startCount = 1;
	private StringBuffer pagingUtil;
	
	public PagingUtil(HashMap<String, Object> arg) {
		
        int curPage = Integer.parseInt(arg.get("currentPage").toString());
		int tot = Integer.parseInt(arg.get("totArticle").toString());
		int listSu = CommonVariable.PAGE_PER_NAVIGATOR;
		int groupSu = CommonVariable.PAGE_GROUP_CNT;
		int curGroup = Integer.parseInt(CommonUtil.strNullChk(arg.get("currentGroup").toString(), "1"));
		
		if (arg.get("page_per_navigator") != null) {
			listSu = Integer.parseInt(CommonUtil.strNullChk(arg.get("page_per_navigator").toString(), "0"));
		}		
		
		int totPage = 1;
        int group = 1;
        int start = 1;
        int end = 1;
        int imsi = 0;
        int lastGroup = 1;
		
        try {
        	if(curPage == 0) curPage = 1;
    		group = curGroup;
    		if(group ==0) group = 1;
    		imsi = tot % listSu;
    		 if (imsi == 0 ) {
             	totPage = (int)Math.ceil((double)((tot-1) / listSu+0.9));
             }else{
             	 totPage = ((tot-1) / listSu) +1;
             }
    	   
    		start = (group * groupSu) - groupSu + 1;
    		end= start + groupSu ;
    		if (totPage < curGroup*groupSu ) end = totPage+1;
          
            int prevV = group - 1;
            int prevP = start - 1;
			pagingUtil = new StringBuffer();
			pagingUtil.append("<div class=\"paging\">");
			pagingUtil.append("<span class=\"icon arrow moreleft\"><a href=\"javascript:goPage('1', '1');\" class=\"screen_out\">맨처음</a></span>");

            if(prevV == 0) prevV = 1;
            if(prevP == 0) prevP = 1;
			pagingUtil.append("<span class=\"icon arrow left\"><a href=\"javascript:goPage('").append(prevP).append("', '").append(prevV).append("');\" class=\"screen_out\">이전</a></span>");
					
            for(int i = start; i < end; i ++) {
  
            	if(i == curPage) {
    				pagingUtil.append("<span class=\"pg\">").append(i).append("</span>");
    			}else {
    				pagingUtil.append("<a href=\"javascript:goPage('").append(i).append("', '").append(group).append("');\" title=\"").append(i).append("페이지 이동\" class=\"pg\">").append(i).append("</a>");
    			}

    			if (i < end-1)
    				pagingUtil.append("<span class=\"icon bar\"></span>");
            }
            
            if(totPage > end - 1) {
            	int nextP = end;
    			int nextV = group + 1;
    			if ((totPage % groupSu) == 0){
    				lastGroup = (totPage / groupSu);
    			} else {
    				lastGroup = (totPage / groupSu) + 1;
    			}
	            pagingUtil.append("<span class=\"icon arrow right\"><a href=\"javascript:goPage('").append(nextP).append("', '").append(nextV).append("');\" class=\"screen_out\">다음</a></span>");
				pagingUtil.append("<span class=\"icon arrow moreright\"><a href=\"javascript:goPage('").append(totPage).append("', '").append(lastGroup).append("');\" class=\"screen_out\">맨끝</a></span>");
            }else {
            	pagingUtil.append("<span class=\"icon arrow right disabled\"><span class=\"screen_out\">다음</span></span>");
				pagingUtil.append("<span class=\"icon arrow moreright disabled\"><span class=\"screen_out\">맨끝</span></span>");
         	}
        } catch (Exception e) {
        	
        }

		pagingUtil.append("</div>");
	}
	
	public int getStartCount() {
		return startCount;
	}
	public StringBuffer getPagingUtil() {
		return pagingUtil;
	}	
}